/**
 * 参数设置弹窗控制器
 */
import tem from './index.html'
import './index.less'
import $_fn from '~plugins'
class paramsetCtrl {
  constructor($scope,$rootScope,http,$ui,$state,$location,$timeout) {
    $scope.param = {}
    $scope.redErr = {}
    $scope.option = {}
    console.log('$scope.params', $scope.params,$rootScope)
    $scope.cancel = function () {
      $rootScope.$uiDirectiveModal.close() //关闭方法
      // window.location.reload();
      if($rootScope.refreshPage){
        $rootScope.refreshPage(1,{})
      }
    }
    //警告提示
    $scope.errorToast = function(text) {
      $ui.toast({text:text,type:'warning',position:'center',appendTo:'.wp-container'});
    }
    //成功提示
    $scope.successToast = function(text) {
        $ui.toast({text:text,type:'success',position:'center',appendTo:'.wp-container'});
    }
    $scope.remHight = function(){                                                 //去除报错高亮
      for(var i in $scope.redErr){
        $scope.redErr[i] = false
      }
    }
    $scope.init = function(){
      //param_id =1,2,3   分别为职业身份 ，申请期限，教育程度
      $scope.url;
      if($scope.params.param_id == 1){
        $scope.url = 'addOrUpdatePosition.do'
        $scope.paramType='position'
      }
      else if($scope.params.param_id == 2){
        $scope.url = 'addOrUpdateApplyTerm.do'
        $scope.paramType='applyTerm'
      }
      else{
        $scope.url = 'addOrUpdateEducationLevel.do'
        $scope.paramType='educationLevel'
      }

      // $scope.data.time_num = '1'
      $scope.option.timeUnit = [{name:'月',id:'1'},{name:'年',id:'2'}]

    }
    $scope.init()
    $scope.deltItem = function(key,item){
      console.log('deltItem',key,item)
      if(item.id){
        http.post('delParamById.do',{'id':item.id,'paramType':$scope.paramType},function (data) {
          if(data.errcode == 1){
            $scope.successToast(data.errmsg)
            $scope.$apply(function(){
              $scope.params.list.splice(key,1)
            })
          }else{
            $scope.errorToast(data.errmsg)
          }
        })
      }else{
        $scope.params.list.splice(key,1)
      }
    }
    $scope.pushItem = function(){
      let item = {'sort':'', 'time_num':'','type':'1'}
      $scope.params.list.push(item)
      console.log('pushItem', $scope.params.list)
    }
    $scope.ok=function (state) {

      let formData = {}
      //param_id =1,2,3   分别为职业身份 ，申请期限，教育程度
      if($scope.params.param_id == 1){
        if($scope.params.list && $scope.params.list.length>0){
          for(let i=0; i<$scope.params.list.length; i++){
            $scope.params.list[i].position_name = $scope.params.list[i].param_name
          }
        }
        formData.positions = JSON.stringify($scope.params.list)
      }
      //申请期限
      else if($scope.params.param_id == 2){
        if($scope.params.list && $scope.params.list.length>0){
          for(let i=0; i<$scope.params.list.length; i++){
            if($scope.params.list[i].type==1){
              //期数单位为月
              $scope.params.list[i].num = $scope.params.list[i].time_num
              $scope.params.list[i].at_name = $scope.params.list[i].time_num+'个月'
            }
            if($scope.params.list[i].type==2){
              //期数单位为年时转为月
              $scope.params.list[i].num = parseInt($scope.params.list[i].time_num*12)
              $scope.params.list[i].at_name = $scope.params.list[i].time_num+'年'
            }
          }
        }
        formData.applyTerms = JSON.stringify($scope.params.list)
      }
      else{
        if($scope.params.list && $scope.params.list.length>0){
          for(let i=0; i<$scope.params.list.length; i++){
            $scope.params.list[i].education = $scope.params.list[i].param_name
          }
        }
        formData.educationLevels = JSON.stringify($scope.params.list)
      }

      http.post($scope.url, formData, function (data) {
        if(data.errcode == 1){
          $scope.cancel()
          $scope.successToast(data.errmsg)
        }else{
          $scope.errorToast(data.errmsg)
        }
      })
    }
  }
}


paramsetCtrl.$inject = ['$scope','$rootScope','http','$ui','$state','$location','$timeout','$interval']
module.exports = angular.module('directive.paramset', []).directive('paramset', function() {
  return {
    restrict: 'E',
    template: tem,
    replace:true,
    controller: paramsetCtrl,
    scope: {
      params: '<'
    },
    link:function(){
      $_fn.clickBind()
    }
  }
}).name